<?php

namespace org;

class PaycheckSdk {

    const API_URL = 'http://bbc.ypytf.com/baoguan/payapi.php'; //配置api接口地址

    static function send_request($channel, $request) {
        $url = self::API_URL . '?a=pay';
        return self::curl_post($url, array('channel' => $channel, 'request' => $request));
    }

    static function send_response($channel, $param = array()) {
        $url = self::API_URL . '?a=pay_back';
        if ($channel == 'weixin') {
            $response = file_get_contents('php://input');
        } else {
            $response = json_encode($param ? array_merge($_POST, $param) : $_POST);
        }
        return self::curl_post($url, array('channel' => $channel, 'response' => $response));
    }

    static function send_baoguan($orderno, $ver_dept, $pay_transaction_id = '') {
        $url = self::API_URL . '?a=baoguan';
        $param = array(
            'orderno' => $orderno,
            'ver_dept' => $ver_dept,
            'pay_transaction_id' => $pay_transaction_id
        );
        return self::curl_post($url, $param);
    }

    static function curl_post($url = '', $data = array()) {
        $postfield = $s = '';
        if (!empty($data)) {
            if (is_array($data)) {
                foreach ($data as $k => $v) {
                    $postfield .= $s . $k . '=' . rawurlencode($v);
                    $s = '&';
                }
            }
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2)");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        if (stripos($url, 'https://') !== FALSE) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        $value = curl_exec($ch);
        if ($err = curl_error($ch)) {
            
        }
        curl_close($ch);
        return $value;
    }

}
